<%@ LANGUAGE="VBSCRIPT" %>
<%

dim xmlfile,xslfile,outfile
xmlfile=Request.QueryString("xml")
xslfile=Request.QueryString("xsl")
outfile=trim(Request.QueryString("name"))
if outfile="" then outfile="export"
if xmlfile="" then
  response.write "ERROR: expected url for xml source document"
elseif xslfile="" then
  response.write "ERROR: expected url for xsl transformation"
else
  doTransform replace(xmlfile,":",""), xslfile & "2xl.xsl"
end if


sub doTransform(ByVal xmlfilename, ByVal xslfilename)
  dim xml,xsl
  
  if not loadXmlDoc(xml,xmlfilename) then exit sub
  if not loadXmlDoc(xsl,xslfilename) then exit sub
  Response.AddHeader "Content-Disposition","attachment; filename=" & outfile & ".xls"
  Response.ContentType = "application/ms-excel"
  'Response.ContentType="application/vnd.ms-excel"
  'Response.ContentType = "application/xml"   ' for debugging
  xml.transformNodeToObject xsl, Response
  set xml = nothing
  set xsl = nothing
end sub

  
function loadXmlDoc(xmldoc, ByVal filename)
  on error resume next
  set xmldoc = Server.CreateObject("Msxml2.DomDocument")
  xmldoc.async = false
  'xmldoc.setProperty "ServerHTTPRequest", true
  'xmldoc.load(filename)
  xmldoc.load(Server.MapPath(filename))
  'Check for a successful load
  if Err.Number <> 0 then
    Response.Write "<p>"
    Response.Write "<strong>Error # " & hex(Err.Number) & " was generated by " & Err.Source & "</strong><hr>"
    Response.Write "File: " & Server.HTMLencode(filename) & "<br>"
    Response.Write Err.Description
    Response.Write "</p>"
  elseif xmldoc.parseerror.errorcode <> 0 then 
    Response.Write "<p>"
    Response.Write "<strong>Error loading XML document</strong><hr>"
    Response.Write "File: " & Server.HTMLencode(filename) & "<br>"
    Response.Write "Error Code: " & xmldoc.parseerror.errorcode & "<br>"
    Response.Write "Reason: " & xmldoc.parseerror.reason
    Response.Write "</p>"
    Response.End
    loadXmlDoc=false
  else
    loadXmlDoc=true
  End If
end function
  
%>
